/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 */

package com.xjw.controller;

import com.xjw.annotation.FXController;
import com.xjw.annotation.FXWindow;
import com.xjw.coreutil.common.PropUtil;
import com.xjw.coreutil.http.JsonUtil;
import com.xjw.entity.*;
import com.xjw.mapper.ChessMapper;
import com.xjw.service.FiveChess;
import com.xjw.service.PlayAction;
import com.xjw.tool.ArrayUtil;
import com.xjw.tool.MybatisUtils;
import com.xjw.utils.ToolUtil;
import com.xjw.websocket.WebSocketClientUtil;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.ComboBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author xwx1052336
 * @since 2021/10/11
 */
@FXController(path = "fxml/chess.fxml")
@FXWindow(title = "智能五子棋", icon = "image/icon.png")
public class ChessController extends FXBaseController implements Initializable {

    @FXML
    private Canvas canvas;

    @FXML
    public Text goBangIdEle;

    @FXML
    private Text userNameEle;

    private GraphicsContext gc;

    /**
     * 棋盘大小 (width -1) * (height -1)
     */
    private final FiveChess fiveChess = new FiveChess(16, 16, 28.0);

    private PlayAction playAction;

    /**
     * 对局信息
     */
    public ChessGame chessGame;

    @FXML
    private Text currentSideEle;

    @FXML
    public ComboBox<String> modelEle;

    private WebSocketClientUtil util;

    public void onClose() {
        util.logOut();
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        double cell = fiveChess.getCellLen();
        drawPane(cell);
        drawChess(cell);
        playAction = new PlayAction(fiveChess, this);
        canvas.setOnMouseClicked(playAction);
        chessGame = playAction.createGame();
        modelEle.getItems().addAll(ChessType.getMsgs());
        modelEle.setValue(ChessType.PTOR.getMsg());
        init();
    }

    private void init() {
        PropUtil propUtil = PropUtil.getInstance(PropUtil.INITDATA);
        ToolUtil.execAsync(()->{
            String obj = propUtil.getValue("userInfo");
            WebSocketUser user = JsonUtil.parseBean(obj, WebSocketUser.class);
            if (user != null) {
                userNameEle.setText(user.getUserName());
            }
            String url = "ws://180.76.134.142:8080/infocollect/gobang";
            util = WebSocketClientUtil.get(url, user);
            util.login();
        }, 1000L);
    }

    /**
     * 悔棋
     */
    @FXML
    private void regretChess() {
        ChessMapper chessMapper = MybatisUtils.getMapper(ChessMapper.class);
        ChessGameInfo chessGameInfo = chessMapper.getLast2GameInfo(chessGame.getTaskId());
        if (chessGameInfo == null) {
            return;
        }
        int [][] lastChess = ArrayUtil.stringToArray(chessGameInfo.getMap(),15,15);
        PlayAction.map = lastChess;
        char[][] chess = ArrayUtil.intArrToChar(lastChess);
        fiveChess.setChess(chess);
        drawPane(fiveChess.getCellLen());
        drawChess(fiveChess.getCellLen());
        fiveChess.changeSide();
        chessMapper.deleteLastGameInfo(chessGameInfo.getTaskId());
    }

    @FXML
    private void initPane(){
        playAction.initPane();
        chessGame = playAction.createGame();
    }

    /**
     * 画棋盘
     *
     * @param cell 格子大小
     */
    public void drawPane(double cell) {
        gc = canvas.getGraphicsContext2D();
        gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
        // 绘制棋盘
        gc.setStroke(Color.BLACK);
        for (int i = 1; i < fiveChess.getWidth() - 1; i++) {
            for (int j = 1; j < fiveChess.getHeight() - 1; j++) {
                // 清理一个矩形取区域的内容
                gc.strokeRect(100 + i * cell, 100 + cell * j, cell, cell);
                if(i == 1 ){
                    gc.fillText(j+"",100 + i * cell - cell/2, 100 + cell * j);
                    gc.fillText(j+"", 100 + cell * j, 100 + i * cell - cell/2);
                }
            }
        }
    }

    /**
     * 根据数组中存储的棋子，画棋盘
     *
     * @param cell 格子大小
     */
    public void drawChess(double cell) {
        currentSideEle.setText(fiveChess.getCurrentSide() == 'B' ? "黑子" : "白子");
        char[][] chess = fiveChess.getChess();
        for (int i = 0; i < fiveChess.getHeight(); i++) {
            for (int j = 0; j < fiveChess.getWidth(); j++) {
                if (chess[i][j] == 'B') {
                    // 设置填充色
                    gc.setFill(Color.BLACK);
                    gc.fillOval(100 + i * cell - cell / 2, 100 + j * cell - cell / 2, cell, cell);
                } else if (chess[i][j] == 'W') {
                    gc.setFill(Color.WHITE);
                    // 填充椭圆
                    gc.fillOval(100 + i * cell - cell / 2, 100 + j * cell - cell / 2, cell, cell);
                    // 绘制轮廓
                    gc.strokeOval(100 + i * cell - cell / 2, 100 + j * cell - cell / 2, cell, cell);
                }
            }
        }
    }

}
